package com.rent.room.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;

import java.util.Date;
import java.util.List;


import com.rent.apartment.domain.ApartmentInfo;
import com.rent.reserve.domain.Reserve;
import com.rent.reserve.service.IReserveService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.rent.common.annotation.Log;
import com.rent.common.enums.BusinessType;
import com.rent.room.domain.RoomInfo;
import com.rent.room.service.IRoomInfoService;
import com.rent.common.core.controller.BaseController;
import com.rent.common.core.domain.AjaxResult;
import com.rent.common.utils.poi.ExcelUtil;
import com.rent.common.core.page.TableDataInfo;
import com.rent.apartment.service.IApartmentInfoService;

/**
 * 房间信息Controller
 * 
 * @author cooper
 * @date 2024-10-09
 */
@Controller
@RequestMapping("/room/roomInfo")
public class RoomInfoController extends BaseController
{
    private String prefix = "room/roomInfo";

    @Autowired
    private IReserveService reserveService;

    @Autowired
    private IRoomInfoService roomInfoService;

    @Autowired
    private IApartmentInfoService apartmentInfoService;
    private String startDateStr;
    private String endDateStr;

    @RequiresPermissions("room:roomInfo:view")
    @GetMapping()
    public String roomInfo()
    {
        return prefix + "/roomInfo";
    }

    /**
     * 查询房间信息列表
     */
    @RequiresPermissions("room:roomInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RoomInfo roomInfo)
    {
        startPage();
        List<RoomInfo> list = roomInfoService.selectRoomInfoList(roomInfo);
        return getDataTable(list);
    }

    /**
     * 导出房间信息列表
     */
    @RequiresPermissions("room:roomInfo:export")
    @Log(title = "房间信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RoomInfo roomInfo)
    {
        List<RoomInfo> list = roomInfoService.selectRoomInfoList(roomInfo);
        ExcelUtil<RoomInfo> util = new ExcelUtil<RoomInfo>(RoomInfo.class);
        return util.exportExcel(list, "房间信息数据");
    }

    /**
     * 新增房间信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存房间信息
     */
    @RequiresPermissions("room:roomInfo:add")
    @Log(title = "房间信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RoomInfo roomInfo)
    {
        return toAjax(roomInfoService.insertRoomInfo(roomInfo));
    }

    /**
     * 修改房间信息
     */
    @RequiresPermissions("room:roomInfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RoomInfo roomInfo = roomInfoService.selectRoomInfoById(id);
        mmap.put("roomInfo", roomInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存房间信息
     */
    @RequiresPermissions("room:roomInfo:edit")
    @Log(title = "房间信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RoomInfo roomInfo)
    {
        return toAjax(roomInfoService.updateRoomInfo(roomInfo));
    }

    /**
     * 删除房间信息
     */
    @RequiresPermissions("room:roomInfo:remove")
    @Log(title = "房间信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] idStrings = ids.split(",");
        Long[] longIds = Arrays.stream(idStrings)
                .map(idStr -> Long.parseLong(idStr.trim()))
                .toArray(Long[]::new);

        return toAjax(roomInfoService.deleteRoomInfoByIds(longIds));
    }  
    
    /**
     * 预约房间详情
     */
    @RequiresPermissions("room:roomInfo:reserve")
    @GetMapping("/reserve/{id}")
    public String reserve(@PathVariable("id") Long id, ModelMap mmap)
    {
        RoomInfo roomInfo = roomInfoService.selectRoomInfoById(id);
        mmap.put("roomInfo", roomInfo);
        return prefix + "/reserve";
    }

    //10/16
    @RequiresPermissions("room:roomInfo:reserve")
    @Log(title = "房间预约", businessType = BusinessType.INSERT)
    @PostMapping("/reserve")
    @ResponseBody
    public AjaxResult reserve(@RequestParam("roomId") Long roomId,
                              @RequestParam("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                              @RequestParam("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        try {
            int result = roomInfoService.insertReserve(roomId, startDate, endDate);
            return toAjax(result);
        } catch (Exception e) {
            logger.error("预约失败", e);
            return AjaxResult.error("预约失败：" + e.getMessage());
        }
    }
}
